﻿/**********************************************************
 ●项目：Smartkernel
 ●公司：智能核心信息技术研发中心
 ●作者：曹艳白
 ●邮箱：caoyanbai@gmail.com
 ●创建：2008-08-10
**********************************************************/
using System;
using System.Runtime.InteropServices;
using Smartkernel.Net;

namespace Smartkernel
{
    /// <summary>
    /// 智能Win32API的封装：提供对系统Win32Api函数的封装
    /// </summary>
    public static partial class SmartWin32API
    {
        #region 公有方法
        /// <summary>
        /// 系统发出嘟嘟声
        /// </summary>
        /// <param name="frequency">频率</param>
        /// <param name="duration">间隔</param>
        /// <returns>调用结果</returns>
        [DllImport("kernel32.dll")]
        public static extern int Beep(int frequency, int duration);
        /// <summary>
        /// 通过IP地址获得MAC地址，IP地址可以是远程主机，本机未联网时，获取不到。这个方法有问题，尽量不要使用
        /// </summary>
        /// <param name="ip">IP地址</param>
        /// <returns>MAC地址</returns>
        public static string GetMAC(string ip)
        {
            Int32 ldest = inet_addr(ip);
            Int32 lhost = inet_addr("");
            Int64 macinfo = new Int64();
            Int32 len = 6;
            int res = SendARP(ldest, 0, ref macinfo, ref len);
            string macSrc = macinfo.ToString("X");
            while (macSrc.Length < 12)
            {
                macSrc = macSrc.Insert(0, "0");
            }
            string mac = "";
            for (int i = 0; i < 11; i++)
            {
                if (0 == (i % 2))
                {
                    mac = mac.Insert(0, macSrc.Substring(i, 2));
                }
            }
            return mac;
        }
        /// <summary>
        /// 获得网络状态标识
        /// </summary>
        /// <param name="lpdwFlags">标识</param>
        /// <param name="dwReserved">参数</param>
        /// <returns>调用情况</returns>
        [DllImport("wininet", CharSet = CharSet.Auto)]
        public static extern bool InternetGetConnectedState(ref SmartConnectionState lpdwFlags, int dwReserved);
        #endregion
    }
    public static partial class SmartWin32API
    {
        #region 私有方法
        [DllImport("Iphlpapi.dll")]
        private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
        [DllImport("Ws2_32.dll")]
        private static extern Int32 inet_addr(string ip);
        #endregion
    }
}
